<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>评论回车发布</title>
  <style>
    .wrapper {
      min-width: 400px;
      max-width: 800px;
      display: flex;
      justify-content: flex-end;
    }

    .avatar {
      width: 48px;
      height: 48px;
      border-radius: 50%;
      overflow: hidden;
      background: url(../../pic/bilibilipinglun.png) no-repeat center / cover;
      margin-right: 20px;
    }

    .wrapper textarea {
      outline: none;
      border-color: transparent;
      background: #f5f5f5;
      resize: none;
      /* 右下角不能拉动 */
      border-radius: 4px;
      flex: 1;
      padding: 10px;
      height: 30px;
      transition: all .5s;
    }

    .wrapper textarea:focus {
      border-color: #e4e4e4;
      background: #fff;
      height: 100px;
    }

    .wrapper button {
      background: #00aeec;
      color: #fff;
      border: none;
      border-radius: 4px;
      margin-left: 10px;
      width: 70px;
      cursor: pointer;
    }

    .wrapper button:hover {
      background: pink;
    }

    .wrapper .total {
      margin-right: 80px;
      color: #999;
      margin-top: 5px;
      opacity: 0;
      transition: all .5s;
    }

    .list {
      min-width: 400px;
      max-width: 800px;
      display: flex;
    }

    .list .item {
      width: 100%;
      display: flex;
    }

    .list .item .info {
      flex: 1;
      border-bottom: 1px solid #e4e4e4;
      padding-bottom: 10px;
    }

    .list .item p {
      margin: 0;
    }

    .list .item .name {
      color: #F87299;
      font-size: 14px;
      font-weight: bold;
    }

    .list .item .text {
      color: #333;
      padding: 10px 0;
    }

    .list .item .time {
      color: #999;
      font-size: 12px;
    }
  </style>
</head>

<body>
  <div class="wrapper">
    <i class="avatar"></i>
    <textarea name="" id="tx" placeholder="发一条友善的评论" rows="2" maxlength="200"></textarea>
    <button>发布</button>
  </div>
  <div class="wrapper">
    <span class="total">0/200字</span>
  </div>
  <div class="list">
    <div class="item" style="display: none;">
      <i class="avatar"></i>
      <div class="info">
        <p class="name">清风徐来</p>
        <p class="text">大家辛苦了，感谢各位大大的努力</p>
        <p class="time">2025-5-20 20:13:14</p>
      </div>
    </div>
  </div>

  <script>
    const tx = document.querySelector('#tx')
    const total = document.querySelector('.total')
    const item = document.querySelector('.item')
    const text = document.querySelector('.text')
    const button = document.querySelector('button')

    // 1.当我们文本域获得了焦点，就让 total 显示出来
    tx.addEventListener('focus', function () {
      total.style.opacity = 1
    })
    // 2.当我们文本域失去了焦点，就让 total 隐藏出来
    tx.addEventListener('blur', function () {
      total.style.opacity = 0
    })
    // 3.检测用户输入
    tx.addEventListener('input', function () {
      total.innerHTML = `${tx.value.length}/200字`
    })
    // 4.当最开始光标文本框里面时，提示"评论输入中..."
    tx.addEventListener('focus', function () {
      tx.placeholder = '评论输入中...'
    })
    tx.addEventListener('blur', function () {
      tx.placeholder = '发一条友善的评论'
    })
    // 5.按下回车发布评论
    tx.addEventListener('keyup', function (e) {
      // 只有按下的是回车键，才会触发
      if (e.key === 'Enter') {
        if (tx.value.trim()) {
          //注意：
          // 如果全是空格，则重置输入操作;如果不为空或文本两端有空格，则自动清空，只输出文本
          // console.log(tx.value)
          // console.log(tx.value.trim())
          item.style.display = 'flex'
          text.innerHTML = tx.value
          total.innerHTML = `0/200字`
        }
        tx.value = ''  //等我们按下回车键，结束，清空文本域
      }
    })
    // 或鼠标点击“发布”按钮也可以
    button.addEventListener('click', function () {
      if (tx.value.trim()) {
        // if ('')
        //注意：上面这俩个写法也可以  console.log(tx.value.trim() !== '') 结果为true
        // 所以tx.value.trim() !== ''  
        // 字符串只有''空才是false，输入文本不为空是true，执行if里面的代码输出文本
        item.style.display = 'flex'
        text.innerHTML = tx.value
        total.innerHTML = `0/200字`
      }
      tx.value = ''  //等我们按下回车键，结束，清空文本域
    })
  </script>
</body>

</html>